/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_pictrue_view.js                                 
*  Description  : 개별클럽 사진게시판 상세보기
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-15  이윤노    Created.  
*  2012-05-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	// 상단 타이틀
	function clsClubTitle()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsClubTitle.prototype = {
		_init: function(){
			var $layer = $("div#container>section#main_content>div.golf_board article.depth2_tab");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$tabs": $layer.find("li")
			}
			
			$layer.data("idx", null); // 인덱스로 관리
			
			this.display();
		}
		, setIdx: function(val){
			
			var $layer = this._HTMLDom["$layer"];
			
			if(val == $layer.data("idx")) return false;
			
			$layer.data("idx", val);
			
			this.display();
			return true;
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			var idx = $layer.data("idx");
			
			// 현재 인덱싱된 타이틀만 on설정
			$tabs.find("img").each(function(n){
				var imgSrc = $(this).attr("src");
				var szImgName = imgSrc.replace(/(.*)\/(.*).gif/g, function(){
					return arguments[1] + "/" + arguments[2].replace("_on", "");
				});
				
				$(this).attr("src", szImgName + ((idx == n) ? "_on":"") + ".gif");
			});
		}
		// 탭 버튼
		, $getsBtnTab: function(){
			var $layer = this._HTMLDom["$layer"], $tabs = this._HTMLDom["$tabs"];
			
			return $tabs.find("a");
		}
	}
	
	//상단메뉴
	function clsTopMenuInfo(){
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsTopMenuInfo.prototype = {
		_init: function(){
			var $layer = $("div#container nav ul.self_club_gnb");			
			this._HTMLDom = {
				"$layer":$layer
			}			
		}
		, getBtnClubHome: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:first");
		}
		, getBtnClubField: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(1)");
		}
		, getBtnClubBoard: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(2)");
		}
		, getBtnClubPic: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(3)");
		}
		, getBtnClubAdmin: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("li:eq(4)");
		}
	}
	
	// 클럽게시판상세
	function clsClubInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsClubInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_view")			
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			fnLoadingJS("../js/jquery.cycle.js", function(){
				//alert(1);
			});
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $imgSmall = $layer.find("table tbody div.img_small ul");
			var $imgBig = $layer.find("table tbody p.img_big")
			
			$layer.find("dl.view_tit dt").contents().eq(0).remove().end().end().prepend(this.infoData["제목"]);
			$layer.find("dl.view_tit dt span").html("(" + this.infoData["댓글수"] + ")");
			$layer.find("dl.view_tit dd span:eq(0) img").attr("src", this.infoData["작성자"]["이미지"]);
			$layer.find("dl.view_tit dd span:eq(1)").html(this.infoData["작성자"]["닉네임"]);
			$layer.find("table thead th:eq(1) span:eq(1)").contents().eq(0).remove().end().end().prepend(this.infoData["작성일"])
			$layer.find("table thead th:eq(1) span:eq(1) span").html(this.infoData["작성시"]);
			$layer.find("table thead th:eq(2) span:eq(1)").html(this.infoData["조회수"]);
			
			
			//이미지 처리 시작
			$imgBig.find("img[alt=이미지]").after("<span id='idPic' style='width: 480px; height: 320px; overflow: visible; margin-left: 220px; float: left; position: relative;'></span>").remove();
			
			$imgSmall.empty(); 
			
			for(var i=0;i<this.infoData["업로드이미지"].length;i++){
				$imgBig.find("span#idPic").append("<img src='" + this.infoData["업로드이미지"][i] + "' style='' />");				
			}
			
			$imgBig.find("span#idPic").cycle({ 
			    fx:     'shuffle', 
			    speed:  'fast', 
			    timeout: 0, 
			    pager:  $imgSmall, //썸네일 생성 상위태그
			    pagerAnchorBuilder: function(idx, slide) {
			    		var img_src = slide.src.replace(/(.*)\/(.*).gif/g, function(){return arguments[1] + "/" + arguments[2].replace("480_315", "105_70") + ".gif"});
			    		//썸네일 이미지태그
			    		return '<li><a href="#"><img src="' + img_src + '" width="105" height="70" /></a></li>'; 
			        
			    } 
			});
			
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("dl:first, table:first").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.prepend("<div id='tmpSpin' style='position:relative; width:600px; height:100px;'></div>");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.find("dl, table").show();
		}
		, $getBtnPictrueDownload: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.find("p.img_big a img");
		}
		
	}
	
	// 클럽게시판댓글
	function clsClubCommentList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsClubCommentList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.board_view div.reply_area")
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("dl:first").clone(true)
			}
			
			$layer.find("dl").remove();
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("dl").remove();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("div.reply_regist").before($element.clone(true));
			}
			
			$layer.find("dl").each(function(n){
				
			});
			
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if($layer.find("div#tmpSpin").length == 0){
				$layer.find("p.white_line").after("<div id='tmpSpin' style='position:relative; width:600px; height:100px;'></div>");
			}
			$layer.find("div#tmpSpin").show().spin();
			
			$layer.find("dl").hide();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div#tmpSpin").hide().spin(false);
			
			$layer.find("dl").show();
		}
		, $getBtnCommentDelete: function(){
			$layer = this._HTMLDom["$layer"];
			return $layer.find("p.reply_del");
		}
		, getCommentData: function(val){
			return this.listData[val];
		}
		, getCommentSubmitData: function(){
			$layer = this._HTMLDom["$layer"];			
			return $layer.find("div.reply_regist textarea");
		}
		, setCommentSubmitData: function(val){
			$layer = this._HTMLDom["$layer"];			
			$layer.find("div.reply_regist textarea").val(val);
		}
		, $getBtnCommnetSubmit: function(){
			$layer = this._HTMLDom["$layer"];			
			return $layer.find("div.reply_regist a");
		}
		, isValid: function(){
			
			return null;
		}
	}
	
	// 수정삭제
	function clsAuthButton()
	{
		this._HTMLDom = null;
		this.bAuth = null;
		
		this._init();
	}
	
	clsAuthButton.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.golf_board article.btn_area_box");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("a").has("img[alt=수정], img[alt=삭제]").hide();
		}
		, setAuth: function(bAuth){
			this.bAuth = bAuth;
			this.display();
		}
		, display: function(){
			if(this.bAuth) this._HTMLDom["$layer"].find("a").has("img[alt=수정], img[alt=삭제]").show();
		}
		, $getBtnModify: function(){
			return this._HTMLDom["$layer"].find("a").has("img[alt=수정]");
		}
		, $getBtnDelete: function(){
			return this._HTMLDom["$layer"].find("a").has("img[alt=삭제]");
		}
		, $getBtnPictrueAllDownload: function(){
			return this._HTMLDom["$layer"].find("a img:eq(0)");
		}
	}
	
	function clsComm()
	{
		this.clubRequest = null;
		this.clubData = null;
		
		this.clubCommentList = null;
		this.clubCommentRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.clubData = {
				"제목": null
				, "댓글수": null
				, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
				, "작성일": null
				, "작성시": null
				, "조회수": null
				, "내용": null
			}
			
			this.clubRequest = {
				"키": null
			}
			
			this.clubCommentList = [
				{
					"키": null
					, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
					, "작성일": null
					, "작성시": null
					, "내용": null
				}
			];
			
			this.clubCommentRequest = {
				"게시판키": null
			}
		}
		, setClubRequest: function(key, val){
			this.clubRequest[key] = val;
		}
		, getClubInfo: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
								
				thisClass.clubData = {
					"제목": "제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다. 제목이 노출됩니다~람쥐~."
					, "댓글수": 2
					, "작성자": {
												"아이디": "sky72"												
												, "이미지": "../images/board/default_50_50.gif"
												, "닉네임": "오스케 짱말짱짱말짱"
											}
					, "업로드이미지": [
														 "../images/board/default_480_315.gif"
														,"../images/board/default_480_315.gif"
														,"../images/board/default_480_315.gif"
														,"../images/board/default_480_315.gif"
														,"../images/board/default_480_315.gif"
														]
					, "작성일": "2012-03-03"
					, "작성시": "17:35"
					, "조회수": 256
					, "글권한": "Y"
					, "내용": "<img  src='../images/board/del.gif' alt=''/><br/><br/>코스 위에 정지하여 있는 흰 볼을 지팡이 모양의 클럽으로 잇달아 쳐서 정해진 홀(球孔)에 넣어 그때까지 소요된 타수(打數)의 많고 적음으로 우열을 겨루는 경기이다. 골프코스는 들판·구릉·산림 등 66만∼100만㎡의 넓은 지역을 이용하여 정형(整形)되어 있고, 해변에 만들어지는 시사이드 코스와 내륙에 만들어지는 인랜드 코스가 있다."
				}
				
				$(thisClass).trigger("evtFinishClubInfo");
			}, 1500);
		}
		, getClubData: function(){
			return this.clubData;
		}
		, setClubCommentRequest: function(key, val){
			this.clubCommentRequest[key] = val;
		}
		, getClubCommentList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.clubCommentList = [
					{
						"키": "001"
						, "작성자": {
							"아이디": "sky72"
							, "이미지": "../images/board/default_50_50.gif"
							, "닉네임": "잭니클라우스우스1"
						}
						, "작성일": "2012.03.30"
						, "작성시": "17:05"
						, "내용": "남녀 1쌍 커플로 갈만한 골프장 어디가 좋을까요? 남녀 2쌍 커플로 갈만한 골프장 어디가 좋을까요 ? 평일에 가보려고 하는데 ... 모두 골프는 중급정도 되고요 ..."
					}
					, {
						"키": "002"
						, "작성자": {
							"아이디": "sky73"
							, "이미지": "../images/board/default_50_50.gif"
							, "닉네임": "잭니클라우스우스2"
						}
						, "작성일": "2012.03.31"
						, "작성시": "14:05"
						, "내용": "남녀 2쌍 커플로 갈만한 골프장 어디가 좋을까요? 남녀 2쌍 커플로 갈만한 골프장 어디가 좋을까요 ? 평일에 가보려고 하는데 ... 모두 골프는 중급정도 되고요 ..."
					}
				];
				
				$(thisClass).trigger("evtFinishClubCommentList");
			}, 1000);
		}
		, clubDelete: function(key){
			var thisClass = this;
			
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				$(thisClass).trigger("evtFinishClubDelete", [ret]);
			}, 500);
		}
		, getClubCommentData: function(){
			return this.clubCommentList;
		}
		, clubCommentDelete: function(q_seq, comment_seq){
			var thisClass = this;
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				$(thisClass).trigger("evtFinishClubCommentDelete", [ret]);
			}, 500);
		}
		, clubCommentInsert: function(q_seq, comment_data){
			var thisClass = this;
			setTimeout(function(){
				var ret = {
					"결과코드": "001"
					, "메시지": null
				}
				
				$(thisClass).trigger("evtFinishClubCommentInsert", [ret]);
			}, 500);
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objClubTitle = null;
		this.objClubInfo = null;
		this.objClubCommentList = null;
		this.objAuthButton = null;
		this.objTopMenuInfo = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objClubTitle = new clsClubTitle();
			this.objClubInfo = new clsClubInfo();
			this.objClubCommentList = new clsClubCommentList();
			this.objAuthButton = new clsAuthButton();
			this.objTopMenuInfo = new clsTopMenuInfo();
			
			this._bind();
			
			// 전체게시글로 default 설정
			this.objClubTitle.setIdx((document.objUtil.getParam("q_my") == "Y") ? 1:0);
			
			// 글정보 받아오기
			if(document.objUtil.getParam("q_seq")){
				this.objClubInfo.spinStart();
				
				this.objComm.setClubRequest("키", document.objUtil.getParam("q_seq"));
				this.objComm.getClubInfo();
				
				// 댓글
				this.objClubCommentList.spinStart();
				this.objComm.setClubCommentRequest("게시판키", document.objUtil.getParam("q_seq"));
				this.objComm.getClubCommentList();
			}
		}
		, _bind: function(){
			var thisClass = this;

			//<--navi 페이지 설정 시작
			//홈
			this.objTopMenuInfo.getBtnClubHome().click(function(event){
				event.preventDefault();						
				document.location.href = "home.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//동호회필드
			this.objTopMenuInfo.getBtnClubField().click(function(event){
				event.preventDefault();						
				document.location.href = "club_field.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//일반게시판
			this.objTopMenuInfo.getBtnClubBoard().click(function(event){
				event.preventDefault();						
				document.location.href = "club_list.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//사진게시판
			this.objTopMenuInfo.getBtnClubPic().click(function(event){
				event.preventDefault();						
				document.location.href = "club_picture_list.html?q_seq=" + (document.objUtil.getParam("q_seq"));
			});
			//관리하기
			this.objTopMenuInfo.getBtnClubAdmin().click(function(event){
				event.preventDefault();						
				document.location.href = "club_admin_info.html?q_seq=" + (document.objUtil.getParam("q_seq"));				
			});
			//navi 페이지 설정 시작 -->
			
			// 글정보 완료
			$(this.objComm).bind("evtFinishClubInfo", function(){
				var thisData = this.getClubData();
				
				thisClass.objClubInfo.spinEnd();
				thisClass.objClubInfo.setInfo(thisData);
				
				//thisClass.objAuthButton.setAuth(document.objUtil.getCookie("member_id") == thisData["작성자"]["아이디"]);
				thisClass.objAuthButton.setAuth(true);
			// 댓글조회완료
			}).bind("evtFinishClubCommentList", function(){
				var thisData = this.getClubCommentData();
				
				thisClass.objClubCommentList.spinEnd();
				thisClass.objClubCommentList.setList(thisData);
			// 삭제완료
			}).bind("evtFinishClubDelete", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				alert("삭제되었습니다.");
				document.location.href = "club_picture_list.html?q_my="+ document.objUtil.getParam("q_my");
						
			// 댓글삭제완료
			}).bind("evtFinishClubCommentDelete", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				alert("댓글이 삭제되었습니다.");
				// 댓글
				thisClass.objClubCommentList.spinStart();
				thisClass.objComm.setClubCommentRequest("게시판키", document.objUtil.getParam("q_seq"));
				thisClass.objComm.getClubCommentList();
			
						
			// 댓글등록완료
			}).bind("evtFinishClubCommentInsert", function(){
				var ret = arguments[1];
				
				document.objGlobal.spinEnd();
				
				alert("댓글이 등록되었습니다.");
				// 댓글
				thisClass.objClubCommentList.setCommentSubmitData("");
				thisClass.objClubCommentList.spinStart();
				thisClass.objComm.setClubCommentRequest("게시판키", document.objUtil.getParam("q_seq"));
				thisClass.objComm.getClubCommentList();
			});
			
			// 글쓰기
			$("div#container section#main_content div.golf_board a").has("img[alt=글쓰기]").click(function(event){
				event.preventDefault();
				
				document.location.href = "club_picture_write.html?q_my=" + document.objUtil.getParam("q_my");
			});
			
			// 목록
			$("div#container section#main_content div.golf_board a").has("img[alt=목록]").click(function(event){
				event.preventDefault();
				
				document.location.href = "club_picture_list.html?q_my=" + document.objUtil.getParam("q_my");
			});
			
			// 수정
			this.objAuthButton.$getBtnModify().click(function(event){
				event.preventDefault();
				
				document.location.href = "club_picture_write.html?q_my=" + document.objUtil.getParam("q_my") + "&q_seq=" + document.objUtil.getParam("q_seq");
			});
			
			// 삭제
			this.objAuthButton.$getBtnDelete().click(function(event){
				event.preventDefault();
				
				if(!confirm("삭제하시겠습니까?")) return;
				
				document.objGlobal.spinStart("deleting...");
				thisClass.objComm.clubDelete(document.objUtil.getParam("q_seq"));
			});
			
			// 댓글삭제
			this.objClubCommentList.$getBtnCommentDelete().live("click", function(event){
				event.preventDefault();
				
				var n = thisClass.objClubCommentList.$getBtnCommentDelete().index($(this));
				var q_seq = document.objUtil.getParam("q_seq")
				var comment_seq = (thisClass.objClubCommentList.getCommentData(n)["키"]);
				
				if(!confirm("삭제하시겠습니까?")) return;
				
				document.objGlobal.spinStart("deleting...");
				thisClass.objComm.clubCommentDelete(q_seq, comment_seq);
			});
			
			// 댓글등록
			this.objClubCommentList.$getBtnCommnetSubmit().click(function(event){
				event.preventDefault();
				
				var q_seq = document.objUtil.getParam("q_seq")
				var comment_data = (thisClass.objClubCommentList.getCommentSubmitData());
								
				document.objGlobal.spinStart("inserting...");
				thisClass.objComm.clubCommentInsert(q_seq, comment_data);
			});
			
			// 사진다운받기
			this.objClubInfo.$getBtnPictrueDownload().click(function(event){
				event.preventDefault();
				alert("사진 다운로드 후루룩~");			
				
				var _url = "../images/board/default_50_50.gif";
        var _input ='<input type="hidden" name="filename" value="'+ _url +'" />';
        
				$('<form action="'+ _url + '" method="post" target="_blank">'+_input+'</form>').appendTo('body').submit().remove();

			});
			
			// 전체사진다운받기
			this.objAuthButton.$getBtnPictrueAllDownload().click(function(event){
				event.preventDefault();
							
				alert("사진 전체 다운로드 후루룩~");

			});
		}
	}
	
	document.objControl = new clsControl();
});